package com.succez.server.utils;

import java.io.Closeable;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class CloseUtil {
    /**
     * 关闭传入的IO流
     * 
     * @param io 任意的需要关闭的io流
     */
    public static void colseIo(Closeable io) {

        if (io != null) {
            try {
                io.close();
                io = null;
            } catch (IOException e) {
            } finally {
                if (io != null) {
                    try {
                        io.close();
                        io = null;
                    } catch (IOException e) {
                        System.out.println("");
                    }
                }
            }
        }

    }

    /**
     * 关闭传入的socket
     * 
     * @param socket 传入的socket连接
     */
    public static void closeSocket(Socket socket) {
        try {
            if (socket != null) {
                socket.close();
                socket = null;
            }
        } catch (IOException e) {
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                    socket = null;
                } catch (IOException e) {
                }

            }
        }
    }

    /**
     * 关闭传入的ServerSocket
     * 
     * @param serverSocket 传入的ServerSocket
     */
    public static void closeServerSocket(ServerSocket serverSocket) {
        try {
            if (serverSocket != null) {
                serverSocket.close();
                serverSocket = null;
            }
        } catch (IOException e) {
        } finally {
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                    serverSocket = null;
                } catch (IOException e) {
                }

            }
        }
    }
}
